<?php
class WjbqjxAction extends WapAction{

    public function _initialize(){
       parent::_initialize();
		session('wapupload',1);
    }

    /**
     * 填表首页
     */
    public function index(){
        if(!$_GET['wecha_id']){
            exit("0");//一定要有微信号才行
        }

        if($_GET['type'] == 'ajax'){
            $data = array();
            $data['company'] = $_POST['company'];
            $data['license'] = $_POST['license'];
            $data['product'] = $_POST['product'];
            $data['resource'] = $_POST['resource'];
            $data['post'] = $_POST['post'];
            $data['mobile'] = $_POST['mobile'];
            $data['wherefrom'] = $_POST['wherefrom'];
            $data['industry'] = $_POST['industry'];
            $data['wecha_id'] = $_GET['wecha_id'];
            M('wjbqjx_user')->add($data);
            exit("1");

        }else {
            $member = M('wjbqjx_user')->where(array('wecha_id'=>$_GET['wecha_id']))->find();
            if($member){
                header("Location: /index.php?g=Wap&m=Wjbqjx&a=edit&token=".$this->token."&wecha_id=".$_GET['wecha_id']);
            }
            $str = '\'qjxproduct\',\'qjxresource\',\'qjxpost\',\'qjxfrom\',\'qjxindustry\'';
            $res = M('wjbconfig')->where(' ckey in ( ' . $str . ' ) ')->select();

            $map = array();
            foreach ($res as $key => $val) {
                $map[$val['ckey']] = unserialize($val['cvalue']);
            }

            $this->assign('map', $map);
            $this->display();
        }
    }

    public function edit(){
        //这个是编辑页面
        if(!$_GET['wecha_id']){
            exit("0");//一定要有微信号才行
        }

        if($_GET['type'] == 'ajax'){
            $id = $_GET['id'];
            $data = array();
            $data['company'] = $_POST['company'];
            $data['license'] = $_POST['license'];
            $data['product'] = $_POST['product'];
            $data['resource'] = $_POST['resource'];
            $data['post'] = $_POST['post'];
            $data['mobile'] = $_POST['mobile'];
            $data['wherefrom'] = $_POST['wherefrom'];
            $data['industry'] = $_POST['industry'];
            $data['wecha_id'] = $_GET['wecha_id'];
            M('wjbqjx_user')->where(array('id'=>$id))->save($data);
            exit("1");

        }else {
            $member = M('wjbqjx_user')->where(array('wecha_id' => $_GET['wecha_id']))->find();
            $this->assign('member', $member);

            $str = '\'qjxproduct\',\'qjxresource\',\'qjxpost\',\'qjxfrom\',\'qjxindustry\'';
            $res = M('wjbconfig')->where(' ckey in ( ' . $str . ' ) ')->select();

            $map = array();
            foreach ($res as $key => $val) {
                $map[$val['ckey']] = unserialize($val['cvalue']);
            }

            $this->assign('map', $map);

            $this->display();
        }
    }

    /**
     * 成员列表
     */
    public function lists(){

        $res =  M('wjbqjx_user')->where(array('status'=>1))->select();

        $this->assign('member',$res);

        $this->display();

    }

    /**
     * 供求信息发布
     */
    public function  msgboard(){

        if(!$this->wecha_id){
            $this->error('来源错误 请用微信浏览器访问...');
        }

        if( isset($_GET['do']) && $_GET['do'] == 'submit' ){

            $data = array();
            $data['title'] = $_POST['title'];
            $data['content'] = $_POST['content'];
            $data['activetime'] = intval($_POST['activetime']);
            $data['wecha_id'] = $this->wecha_id;
            $data['author'] = $_POST['author'];

            $data['releasetime'] = time();
            $data['activetime'] = $data['activetime'] * 24 * 60 * 60;
            $data['activetime'] = $data['releasetime'] + $data['activetime'];//有效时间
            $data['pid'] = 0;
            //$data['wecha_id'] = $this->wecha_id;

            M('wjbqjx_gq')->add($data);
            $this->success("信息提交成功",'/index.php?g=Wap&m=Wjbqjx&a=msgboardshow&token='.$this->token.'&wecha_id='.$this->wecha_id);
        }

        $this->display();

    }

    /**
     * 供求信息列表
     */
    public function msgboardshow(){

        $message = M('wjbqjx_gq')->where(array('pid'=>0))->select();

        $this->assign('message',$message);

        $this->display();

    }

    /**
     * 供求信息详细内容
     */
    public function msginfo(){
        if(!$this->wecha_id){
            exit;
        }
        if($_GET['type'] == 'ajax'){
            $content = $_POST['content'];
            $id = $_GET['id'];
            if( !$content || !$id ){
                exit("0");
            }
            $data['pid'] = $id;
            $data['wecha_id'] = $this->wecha_id;
            $data['content'] = $content;
            $data['releasetime'] = time();
            M('wjbqjx_gq')->add($data);
            exit("1");
        }else {
            $id = $_GET['id'];
            $info = M('wjbqjx_gq')->where(array('id' => $id))->find();

            $this->assign('info', $info);

            //回复信息
            $replies = M('wjbqjx_gq')->where(array('pid' => $info['id']))->select();
            $this->assign('replies', $replies);


            //查寻会员信息
            

            $this->display();
        }

    }

    /**
     * 测试用
     */
    public function ceshiwecha(){
      echo '这个就是测试<br />';
      echo $this->wecha_id;
      echo "<br />这个另外起一行";
    }

    /**
     * 用户发布项目界面
     */
    public function additem(){
        if(!$this->wecha_id){
            $this->error("出点小错误了 请联系管理员 微聚宝信息技术有限公司");
        }

        if( isset($_GET['do']) && $_GET['do'] == 'submit') {
            $data = $_POST['data'];

            $tableData = array();
            $tableData['name'] = $data['name'];
            $tableData['thumb'] = $data['thumb'];
            $tableData['founder'] = $data['founder'];
            $tableData['info'] = $data['info'];
            $tableData['advantage'] = $data['advantage'];
            $tableData['commoninfo'] = $data['commondata'];
            $tableData['status'] = 0;
            $tableData['support'] = 0;
            $tableData['intime'] = time();
            $tableData['validtime'] = $tableData['intime'] + 24 * 60 * 60 * 30;
            $tableData['wecha_id'] = $this->wecha_id;

            M('wjbqjx_item')->add($tableData);

            exit("项目添加成功 请等待审核");

        }

        $this->display();

    }

    /**
     * 我的项目列表界面
     */
    public function myitem(){
        if(!$this->wecha_id){
            $this->error("出点小错误了 请联系管理员 微聚宝信息技术有限公司");
        }

        $res = M('wjbqjx_item')->where(array('wecha_id'=>$this->wecha_id))->select();
        $this->assign('items',$res);

        $this->display();

    }

    /**
     * 项目编辑界面
     */
    public function edititem(){
        if(!$this->wecha_id){
            $this->error("出点小错误了 请联系管理员 微聚宝信息技术有限公司");
        }
        if(!isset($_GET['id'])){
            $this->error("出点小错误了 请联系管理员 微聚宝信息技术有限公司");
        }
        if( isset($_GET['do']) && $_GET['do'] == 'submit') {
            $data = $_POST['data'];

            $tableData = array();
            $tableData['name'] = $data['name'];
            $tableData['thumb'] = $data['thumb'];
            $tableData['founder'] = $data['founder'];
            $tableData['info'] = $data['info'];
            $tableData['advantage'] = $data['advantage'];
            $tableData['commoninfo'] = $data['commondata'];


            M('wjbqjx_item')->where(array('id'=>$_GET['id'],'wecha_id'=>$this->wecha_id))->add($tableData);

            exit("项目修改成功 请等待审核");

        }else {

            $item = M('wjbqjx_item')->where(array('id' => $_GET['id']))->find();


            $this->assign('item', $item);

            $this->display();
        }
    }

    /**
     * 项目列表
     */
    public function itemlist(){

        $items = M('wjbqjx_item')->where(array('status'=>1))->select();

        $content = array();

        foreach( $items as $key => $val ){
            $str = '<div class="name"><span>'.$val['name'].'</span></div><div class="thumb"><a href="/index.php?g=Wap&m=Wjbqjx&a=itemshow&id='.$val['id'].'&token='.$this->token.'"><img src="'.$val['thumb'].'" /></a></div>';
            array_push($content,$str);
        }

        $this->assign('content',$content);

        $this->display();
    }

    public function itemshow(){

        $item = M('wjbqjx_item')->where(array('id'=>$_GET['id']))->find();
        $item['commoninfo'] = htmlspecialchars_decode($item['commoninfo']);
        $this->assign( 'item', $item );

        $this->display();
    }


    /**
     * 支持一下
     */
    public function vote(){
        if(isset($_GET['id'])&&isset($_GET['cell']) && isset($_GET['type'])){
            $id = intval($_GET['id']);
            $cell = $_GET['cell'];
            $type = $_GET['type'];
        }else{
            exit("0");
        }

        $res = M('wjbqjx_itemopp')->where(array('cellphone'=>$cell))->find();
        if($res){
            exit("2");
        }
        if($type == 1) {
            M('wjbqjx_item')->where(array('id' => $id))->setInc('support', 1);
        }else{
            M('wjbqjx_item')->where(array('id'=>$id))->setInc('oppose',1);
        }
        $data = array();
        $data['intime'] = time();
        $data['itemid'] = $id;
        $data['cellphone'] = $cell;
        $data['type'] = $type;

        M('wjbqjx_itemopp')->add($data);
        exit("1");
    }



}